package com.abp.service.impl;

import com.abp.entity.UserBookRating;
import com.abp.mapper.UserBookRatingMapper;
import com.abp.service.UserBookRatingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class UserBookRatingServiceImpl extends ServiceImpl<UserBookRatingMapper, UserBookRating> implements UserBookRatingService {

    private final Random random = new Random();

    @Override
    public void generateUserBookRatings(int userCount, int bookCount) {
        List<UserBookRating> ratingList = new ArrayList<>();

        for (int userId = 1; userId <= userCount; userId++) {
            for (int bookId = 1; bookId <= bookCount; bookId++) {
                if (random.nextDouble() < 0.3) { // 控制部分数据，避免所有用户对所有书评分
                    UserBookRating rating = new UserBookRating();
                    rating.setUserId(userId);
                    rating.setBookId(bookId);
                    rating.setRating(random.nextInt(5) + 1);
                    ratingList.add(rating);
                }
            }
        }

        saveBatch(ratingList);
        System.out.println("用户评分数据生成完毕，共插入：" + ratingList.size() + " 条数据");
    }

}
